package com.heima.article.controller.v1;

import com.heima.apis.article.ApArticleControllerApi;
import com.heima.apis.article.ArticleHomeControllerApi;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.vo.SearchArticleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/article")
public class ApArticleController implements ApArticleControllerApi {
    @Resource
    private ApArticleService articleService;

    /**
     * 保存app文章
     *
     * @param articleDto
     * @return
     */

    @Override
    @PostMapping("/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto articleDto) {
        return articleService.saveAllData(articleDto);
    }

    @GetMapping("/findByArticleId/{id}")
    public ApArticle findByArticleId(@PathVariable("id") Long articleId) {
        return articleService.getById(articleId);
    }

    @GetMapping("/findAllArticles")
    public List<ApArticle> findAllArticles() {
        return articleService.findAllArticles();
    }

    @GetMapping("{id}")
    @Override
    public SearchArticleVo findArticle(@PathVariable Long id) {
        ApArticle article = articleService.getById(id);
        if (article != null) {
            SearchArticleVo searchArticleVo = new SearchArticleVo();
            BeanUtils.copyProperties(article, searchArticleVo);
            return searchArticleVo;
        }
        return null;
    }
}

